Quantum Factorization Algorithm: QFT and Shor Algorithm

Introduce quantum fourier transform (QFT) and Shor algorithm for quantum factorization.

Table of Contents

1. Quantum Fourier Transform

We define Fourier basis on \(D=2^{n}\)-dimensional quantum system.

\[ \ket{\phi_{k}} = \frac{1}{\sqrt{D}} \sum_{i=0}^{D-1} \omega^{jk} \ket{j}, \qquad \omega = e^{2\pi i/D} \]

So the QFT is basically a mapping from computational basis to Fourier basis.

\[ \mathrm{QFT} = \sum_{k=0}^{D-1} \ket{\phi_{k}}\bra{k} \]

1.1. QFT for \(n\) Qubits

Let’s express \(k\) as binary \(k:=k_{1}k_{2}\dots k_{D}\). Then, \(\ket{\phi_{k}}\) can be expressed by tensor products, i.e., it’s a product state.

\[ \ket{\phi_{k}} = \otimes_{l=1}^{n} \frac{\ket{0} + e^{2\pi i \ 0.j_{n-l+1}j_{n-l+2}\dots j_{n}}\ket{1}}{\sqrt{2}} \]

The \(0.j_{i}\dots j_{n}\) is an abbreviation of \(\frac{j_{i}}{2^{1}} + \dots + \frac{j_{n}}{2^{n-i+1}}\)

1.2. Implementing QFT

For an \(n\)-qubit quantum system \(\ket{j} = \ket{j_{1}j_{2}\cdots j_{n}}\), expanding Fourier basis \(\ket{e_{j}}\)1 gives

\[ |e_j\rangle = \left( \frac{|0\rangle + e^{2\pi i (0.j_n)}|1\rangle}{\sqrt{2}} \right) \otimes \left( \frac{|0\rangle + e^{2\pi i (0.j_{n-1}j_n)}|1\rangle}{\sqrt{2}} \right) \otimes \cdots \otimes \left( \frac{|0\rangle + e^{2\pi i (0.j_1 j_2 \dots j_n)}|1\rangle}{\sqrt{2}} \right) \]

Ordering Problem.

As a result, to avoid ordering problem, we can apply quantum gates in a reverse order.

  1. First apply Hadamard gate \(H\) to \(\ket{j_{1}}\). Then, apply gate \(\frac{|0\rangle + e^{2\pi i (0.j_1 j_2 \dots j_n)}|1\rangle}{\sqrt{2}}\) conditioned by \(j_{2}\cdots j_{n}\), here, \(j_{2}\cdots j_{n}\) are still unmodified and are either \(\ket{0}\) or \(\ket{1}\). But to note that, this should be the output state of \(\ket{j_{n}}\)
  2. Repeat step 1. Finally, we apply \(H\), then \(\frac{|0\rangle + e^{2\pi i (0.j_n)}|1\rangle}{\sqrt{2}}\) to \(\ket{j_{n}}\) conditioned on \(\ket{j_{n}}\). But this should be the output state of \(\ket{j_{1}}\).

After applied all gates, we use \(\frac{n}{2}\) SWAP gates to obtain the correct ordering.


2. Quantum Phase Estimation

Problem Specification. We have a unitary matrix \(U\) and its eigenstate \(\ket{u}\), whose corresponding eigenvalue is \(e^{2\pi i\varphi}\). The \(\varphi\in[0,1)\) is the global phase we want:

\[ U\ket{u} = e^{2\pi i \varphi}\ket{u} \]

2.1. Key Trick: Phase Kickback

We apply controlled-gate \(C(U)\) to control bit \(\ket{+}\) and target bit \(\ket{u}\), we get

\[\begin{aligned} & C(U) \left( \frac{\ket{0}+\ket{1}}{\sqrt{2}} \otimes \ket{u} \right) \\ = & \frac{\ket{0}\ket{u} + \ket{1} (U\ket{u})}{\sqrt{2}} \\ = & \left( \frac{\ket{0} + e^{2\pi i \varphi}\ket{1}}{\sqrt{2}} \right) \otimes \ket{u} \end{aligned}\]

So we can see that the global phase \(e^{2\pi i\varphi}\) has been moved to the control bit.

2.2. QPE Algorithm

The input consists of (1) controlled-gate \(C(U)\) as an oracle; (2) an eigenstate \(\ket{u}\) of \(C(U)\) with eigenvalue \(e^{2\pi i \varphi}\). It follows the following procedure.

  1. Prepare 2 registers: master register (stores \(t\) \(\ket{0}^{\otimes t}\)-initialized qubits), input register that stores \(\ket{u}\). The overall system’s composite quantum state is \[ \ket{\psi_{0}} = \ket{0}^{\otimes t} \otimes \ket{u} \]
  2. Apply Hadamard gate to all qubits on master register, to generate equa-weight superposition state. \[ \ket{\psi_{1}} = (H^{\otimes t} \ket{0}^{\otimes t}) = \frac{1}{\sqrt{2^{t}}} \sum_{j=0}^{2^{t}-1} \ket{j}\otimes \ket{u} \]
  3. For \(k=1,2,\cdots,t\), apply \(2^{t-k}\) controlled-gate \(C(U)\) with \(k\)-th qubit on the master register being control bit and the input register being target bit. Or equivalently, apply \(C(U^{2^{t-k}})\) controlled-gate.

    So here, according to \(U\ket{u}=e^{2\pi i \varphi}\ket{u}\), when the control qubit is \(\ket{1}\), the phase is “kicked-back” to the qubit on master register and the input register remains unchanged.

    After all controlled-gates are applied, equivalently, we applied \(U^{j}\) transformation to each computation basis \(\ket{j}\). So the system becomes superposition state under Fourier basis: \[ |\psi_2\rangle = \frac{1}{\sqrt{2^t}} \sum_{j=0}^{2^t-1} e^{2\pi i \varphi j} |j\rangle \otimes |u\rangle \]

  4. Apply inverse QFT (\(FT^{\dagger}\)) to master register, and measure in the computational basis of \(t\) qubits.
  5. Output \(\hat{\varphi}=0.j_{1}j_{2}\cdots j_{t}\)

3. Shor Algorithm

Our goal is to factorize a \(L\)-bit integer \(N\), consider another integer \(x\lt N\) that is coprime with \(N\). The quantum part of Shor’s algorithm finds the order of \(x \mod N\), which is the smallest integer \(r\gt 0\) such that \(f(r)=1, \quad f(a):=x^{a} \mod N\).

The idea is divide-and-conquer. First, reduce factorization of \(N=pq\) to the problem of order-finding of \(x\mod N\); second, reduce order-finding to a phase estimation problem; finally, use QPE to solve phase esitmation problem.

The overall procedure is: we first check if \(N=a^{b}\) for some \(a,b\). Otherwise, we randomly pick a positive integer \(x\) coprime with \(N\). Then, we find out \(r = \mathrm{order}(x,N)\).

  • If \(r\) is odd, abandon the candidate \(x\) and restart;
  • then \(r\) is even, let \(a := x^{r/2} \pmod{N}\), if \(a+1\not\equiv 0\pmod N\), we can conclude that

    \[ \gcd(a+1, N), \gcd(a-1,N) \]

    contains at least one of \(p,q\).

Note that \(a^{2} \equiv x^{r}\pmod{N} \equiv 1\pmod{N}\), thus \((a-1)(a+1) = s\cdot p\cdot q\) for some \(s\).

Therefore, as long as \(a-1,a+1\not\equiv 0\pmod N\), we must have either \(p,q\) contained in \(a-1,a+1\).

The Quantum Speedup. The quantum part happens in \(\mathrm{order}(x,N)\). Suppose we have an oracle

\[ O:\ket{y}\mapsto\ket{(xy) \pmod{N}} \]

We have \(\ket{u_{s}}\) being eigenstates of \(O\):

\[ \ket{u_{s}} = \frac{1}{\sqrt{r}} \sum_{k=0}^{r-1} e^{2\pi i \left( -\frac{ks}{r} \right)} \ket{x^{k} \pmod N} \]

As we have

\[ O\ket{u_{s}} = e^{2\pi i\cdot \frac{s}{r}}\ket{u_{s}} \]

3.1. Constructing QPE

So next, we need to construct \(C(O)\) and \(\ket{u_{s}}\) for some \(s\).

Instead of construct \(O \to C(O) \to O_{QPE}\), we directly construct the QPE oracle over \(t\) qubits for the modular multiplication oracle \(O\). We conclude that

\[ O_{QPE} = \sum_{j=0}^{2^{t}-1} \ket{j}\bra{j} \otimes O^{j} \]

is equal to \(O_{x,N}\), i.e., \(O_{QPE}=O_{x,N}\), where \(O_{x,N}\) is the oracle for \(f(a)=x^{a} \pmod N\). It acts on the system \(\ket{x}\) and ancilla \(\ket{q}\) of length \((\log N)\)-bits.

\[ O_{x,N}:\ket{a}\ket{q}\mapsto\ket{a}\ket{q\cdot x^{a}\pmod N} \]

\(O_{x,N}\) on \(t\)-qubits can be realized with \(O(t^{3})=O(\log^{3} N)\) gates.

Using \[ \frac{1}{\sqrt{r}} \sum_{k} O^{j}\ket{u_{k}} = \ket{x^{j}\pmod N} \]

we can show that

\[ O_{x,N} (\ket{j}\ket{1}) = \ket{j}\ket{x^{j} \pmod N} \]

3.2. Eigenstates for QPE

We cannot directly construct \(\ket{u_{s}}\) since we don’t know \(r\).

Footnotes:

1

just another notation for \(\ket{\phi_{j}}\).

Date: 2026-10-25 Sun

Author: ArcaLunar